/**
 * Copyright (C) 2008 by The Regents of the University of California
 * Redistribution of this file is permitted under the terms of the GNU
 * Public License (GPL).
 *
 * @author Junghoo "John" Cho <cho AT cs.ucla.edu>
 * @date 3/24/2008
 */

#include <cstdio>
#include <iostream>
#include <fstream>
#include "Bruinbase.h"
#include "SqlEngine.h"
#include "RecordFile.h"
#include "BTreeIndex.h"

using namespace std;

// external functions and variables for load file and sql command parsing 
extern FILE* sqlin;
int sqlparse(void);

RC SqlEngine::run(FILE* commandline)
{
  fprintf(stdout, "Bruinbase> ");

  // set the command line input and start parsing user input
  sqlin = commandline;
  sqlparse();  // sqlparse() is defined in SqlParser.tab.c generated from
               // SqlParser.y by bison (bison is GNU equivalent of yacc)

  return 0;
}

RC SqlEngine::select(int attr, const string& table, const vector<SelCond>& cond)
{
  RecordFile rf;   // RecordFile containing the table
  RecordId   rid;  // record cursor for table scanning

  RC     rc;
  int    key;     
  string value;
  int    count;
  int    diff;

  // open the table file
  if ((rc = rf.open(table + ".tbl", 'r')) < 0) {
    fprintf(stderr, "Error: table %s does not exist\n", table.c_str());
    return rc;
  }

  // scan the table file from the beginning
  rid.pid = rid.sid = 0;
  count = 0;
  while (rid < rf.endRid()) {
    // read the tuple
    if ((rc = rf.read(rid, key, value)) < 0) {
      fprintf(stderr, "Error: while reading a tuple from table %s\n", table.c_str());
      goto exit_select;
    }

    // check the conditions on the tuple
    for (unsigned i = 0; i < cond.size(); i++) {
      // compute the difference between the tuple value and the condition value
      switch (cond[i].attr) {
      case 1:
	diff = key - atoi(cond[i].value);
	break;
      case 2:
	diff = strcmp(value.c_str(), cond[i].value);
	break;
      }

      // skip the tuple if any condition is not met
      switch (cond[i].comp) {
      case SelCond::EQ:
	if (diff != 0) goto next_tuple;
	break;
      case SelCond::NE:
	if (diff == 0) goto next_tuple;
	break;
      case SelCond::GT:
	if (diff <= 0) goto next_tuple;
	break;
      case SelCond::LT:
	if (diff >= 0) goto next_tuple;
	break;
      case SelCond::GE:
	if (diff < 0) goto next_tuple;
	break;
      case SelCond::LE:
	if (diff > 0) goto next_tuple;
	break;
      }
    }

    // the condition is met for the tuple. 
    // increase matching tuple counter
    count++;

    // print the tuple 
    switch (attr) {
    case 1:  // SELECT key
      fprintf(stdout, "%d\n", key);
      break;
    case 2:  // SELECT value
      fprintf(stdout, "%s\n", value.c_str());
      break;
    case 3:  // SELECT *
      fprintf(stdout, "%d '%s'\n", key, value.c_str());
      break;
    }

    // move to the next tuple
    next_tuple:
    ++rid;
  }

  // print matching tuple count if "select count(*)"
  if (attr == 4) {
    fprintf(stdout, "%d\n", count);
  }
  rc = 0;

  // close the table file and return
  exit_select:
  rf.close();
  return rc;
}

RC SqlEngine::load(const string& table, const string& loadfile, bool index)
{
    // Do not create an index on the key
    
    FILE *load_file;
    string loadFile_str;
    
    RecordFile recordFile;
    BTreeIndex recordIndex;
    string recordFile_str = table + ".tbl";
    string recordIndex_str = table + ".idx";
    
    // Checks if an index is to be created on the record file
    if(index == true)
    {
        if(recordIndex.open(recordIndex_str, 'w') != 0)
        {
            // @ERROR: Could not open index file for writing
            
            fprintf(stderr, "ERROR: Could not open file - %s\n", 
                    recordIndex_str.c_str());
            return(RC_INVALID_ATTRIBUTE);
        }
    }
    
    // Open the load file
    load_file = fopen(loadfile.c_str(), "r");
    
    if(load_file == NULL)
    {
        // @ERROR: Could not open the load file for reading
        
        fprintf(stderr, "ERROR: Could not open file - %s\n", 
                loadfile.c_str());
        return(RC_FILE_OPEN_FAILED);
    }
    
    // Open the record file
    if(recordFile.open(recordFile_str.c_str(), 'w') != 0)
    {
        // @ERROR: Could not open the record file for writing
        
        fprintf(stderr, "ERROR: Could not open file - %s\n", 
                recordFile_str.c_str());
        return(RC_FILE_OPEN_FAILED);
    }
    
    string loadLine_str;
    char *loadLine = NULL;
    size_t lineSize = -1;
    
    int key;
    string value;
    RecordId rid;
    
    // Iterate through all the lines in the load file
    while(getline(&loadLine, &lineSize, load_file) != -1)
    {
        // Convert the loadLine CString to a C++ string
        loadLine_str.assign(loadLine);
        
        // Free the loadLine CString
        free(loadLine);
        loadLine = NULL;
        
        // Parse the key and value from loadLine
        parseLoadLine(loadLine_str, key, value);
        rid = recordFile.endRid();
        
        if(recordFile.append(key, value, rid) != 0)
        {
            // @ERROR: Could not insert into record file

            fprintf(stderr, "ERROR: Could not insert into record file - "
                    "(%d, \"%s\")\n", key, value.c_str());
            return(RC_FILE_WRITE_FAILED);
        }
        
        // Check if key indexing is enabled
        if(index == true)
        {
            // Insert the record into the index
            if(recordIndex.insert(key, rid) != 0)
            {
                // @ERROR: Could not insert key into index
                
                fprintf(stderr, "ERROR: Could not insert key into index\n");
                return(RC_FILE_WRITE_FAILED);
            }
        }
    }
    
    // Check if key indexing is enabled
    if(index == true)
    {
        if(recordIndex.close() != 0)
        {
            // @ERROR: Could not close access to the index file
            
            fprintf(stderr, "ERROR: Could not close file - %s\n",
                    recordIndex_str.c_str());
            return(RC_FILE_CLOSE_FAILED);
        }
    }
    
    // Close the record file
    if(recordFile.close() != 0)
    {
        // @ERROR: Could not close access to the record file
        
        fprintf(stderr, "ERROR: Could not close file - %s\n", 
                recordFile_str.c_str());
        return(RC_FILE_CLOSE_FAILED);
    }
    
    // Close the load file
    if(fclose(load_file) != 0)
    {
        // @ERROR: Could not close access to the record file
        
        fprintf(stderr, "ERROR: Could not close file - %s\n", loadfile.c_str());
        return(RC_FILE_CLOSE_FAILED);
    }
    
    load_file = NULL;
    
    return(0);
}

RC SqlEngine::parseLoadLine(const string& line, int& key, string& value)
{
    const char *s;
    char        c;
    string::size_type loc;
    
    // ignore beginning white spaces
    c = *(s = line.c_str());
    while (c == ' ' || c == '\t') { c = *++s; }

    // get the integer key value
    key = atoi(s);

    // look for comma
    s = strchr(s, ',');
    if (s == NULL) { return RC_INVALID_FILE_FORMAT; }

    // ignore white spaces
    do { c = *++s; } while (c == ' ' || c == '\t');
    
    // if there is nothing left, set the value to empty string
    if (c == 0) { 
        value.erase();
        return 0;
    }

    // is the value field delimited by ' or "?
    if (c == '\'' || c == '"') {
        s++;
    } else {
        c = '\n';
    }

    // get the value string
    value.assign(s);
    loc = value.find(c, 0);
    if (loc != string::npos) { value.erase(loc); }

    return 0;
}
